package com.quickcancellation.crm.utils;

import com.quickcancellation.crm.pojo.dto.ResponseDto;

import java.util.List;

/**
 * @author 麦翔盛
 * @ClassName: ResponseUtil
 * @Package: com.quickcancellation.crm.utils
 * @description: 响应类工具类
 * @date 2023/2/4 22:00
 */
public class ResponseUtil {

    /**
     * 成功
     *
     * @param count 总数
     * @param data  数据
     * @return
     */
    public static ResponseDto success(long count, Object data) {

        return new ResponseDto(count, data, "200", "success");
    }


    public static ResponseDto success() {

        return new ResponseDto(null, null, "200", "success");
    }


    /***
     * 返回列表数据 自动获取数量
     * @param data
     * @return
     * @param <T>
     */
    public static <T> ResponseDto success(List<T> data) {

        return new ResponseDto((long) data.size(), data, "200", "success");
    }

    /**
     * 失败
     *
     * @param code    响应码
     * @param message 响应信息
     * @return
     */
    public static ResponseDto fail(String code, String message) {

        return new ResponseDto(null, null, code, message);
    }

    /**
     * 默认错误值
     *
     * @param message
     * @return
     */
    public static ResponseDto fail(String message) {

        return new ResponseDto(null, null, "-4000", message);
    }

    /**
     * 默认返回值
     *
     * @return
     */
    public static ResponseDto fail() {

        return new ResponseDto(null, null, "-4001", "操作失败");
    }


    /**
     * 根据结果返回
     *
     * @param result
     * @return
     */
    public static ResponseDto ok(boolean result) {
        return result ? ResponseUtil.success() : ResponseUtil.fail("-5000", "操作失败");
    }


}
